package com.huatai.bpmn.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
import com.huatai.bpmn.entity.BasicEntity;
import com.huatai.bpmn.entity.CompleteConfigEntity;
import com.huatai.bpmn.entity.FileEntity;
import com.huatai.bpmn.entity.vo.CompleteDataVO;
import com.huatai.bpmn.entity.vo.CompleteStructVO;
import com.huatai.bpmn.service.BasicService;
import com.huatai.bpmn.service.CompleteConfigService;
import com.huatai.bpmn.mapper.CompleteConfigMapper;
import com.huatai.bpmn.service.FileService;
import com.huatai.bpmn.utils.SqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 *
 */
@Service
public class CompleteConfigServiceImpl extends ServiceImpl<CompleteConfigMapper, CompleteConfigEntity> implements CompleteConfigService{

	@Autowired
	private FileService fileService;

	@Autowired
	private BasicService basicService;

	@Override
	public Map<String,Object> getCompleteData(Long configId, String processInstanceId) {
		Map<String,Object> resultMap = new HashMap<>();
		CompleteDataVO completeData = new CompleteDataVO();
		CompleteConfigEntity configEntity = getById(configId);
		Map<String, Object> paramMap = getParamMap(configEntity.getParamConfig(), processInstanceId);
		Map<String, Object> dataMap = getDataMap(configEntity.getDataConfig(), processInstanceId);
		List<FileEntity> filesList = getFilesList(configEntity.getFilesConfig(), processInstanceId);
		completeData.setData(dataMap);
		completeData.setFiles(filesList);
		completeData.setParam(paramMap);
		resultMap.put("datas",completeData);
		CompleteStructVO completeStruct = getCompleteStruct(configEntity);
		resultMap.put("structs",completeStruct);

		return resultMap;
	}


	private CompleteStructVO getCompleteStruct(CompleteConfigEntity completeConfig){
		CompleteStructVO completeStruct = new CompleteStructVO();
		//获取字段参数配置信息
		JSONObject paramConfigJson = JSONObject.parseObject(completeConfig.getParamConfig());
		JSONArray paramConfigs = paramConfigJson.getJSONArray("configs");
		completeStruct.setParamStruct(paramConfigs);
		//获取文件配置信息
		completeStruct.setFilesStruct(JSONObject.parseArray(completeConfig.getFilesConfig()));
		//获取表格配置信息
		JSONArray dataConfigArray = JSONObject.parseArray(completeConfig.getDataConfig());
		JSONArray dataConfigList = new JSONArray();
		for (int i = 0; i < dataConfigArray.size(); i++) {
			JSONObject dataConfigJson = dataConfigArray.getJSONObject(i);
			String place = dataConfigJson.getString("place");
			List<BasicEntity> colum = basicService.getColum(place);
			dataConfigJson.put("selectSql",null);
			dataConfigJson.put("column",colum);
			dataConfigList.add(dataConfigJson);
		}
		completeStruct.setDataStruct(dataConfigList);
		return completeStruct;
	}

	/**
	 * 获取字段参数数据
	 * @param paramConfig
	 * @param processInstanceId
	 * @return
	 */
	private Map<String,Object> getParamMap(String paramConfig,String processInstanceId){
		JSONObject paramConfigJson = JSONObject.parseObject(paramConfig);
		String selectSql = paramConfigJson.getString("selectSql");
		if (SqlUtil.checkQuerySql(selectSql)){
			return new HashMap<>();
		}else {
			selectSql += " WHERE 1=1 AND a.business_key = '"+processInstanceId+"'";
		}
		return SqlRunner.db().selectOne(selectSql);
	}

	/**
	 * 获取表格数据
	 * @param dataConfig
	 * @param processInstanceId
	 * @return
	 */
	private Map<String,Object> getDataMap(String dataConfig,String processInstanceId){
		Map<String,Object> dataMap = new TreeMap<>();
		JSONArray dataConfigArray = JSONObject.parseArray(dataConfig);
		for (int i = 0; i < dataConfigArray.size(); i++) {
			JSONObject dataConfigJson = dataConfigArray.getJSONObject(i);
			String placeKey = dataConfigJson.getString("place");
			String selectSql = dataConfigJson.getString("selectSql");
			if (SqlUtil.checkQuerySql(selectSql)){
				dataMap.put(placeKey,new ArrayList<>());
			}else {
				selectSql += " WHERE 1=1 AND business_key = '"+processInstanceId+"'";
				Map<String, Object> placeMap = SqlRunner.db().selectOne(selectSql);
				dataMap.put(placeKey,JSONObject.parseArray((String) placeMap.get(placeKey)));
			}
		}
		return dataMap;
	}

	/**
	 * 获取附件数据
	 * @param filesConfig
	 * @param processInstanceId
	 * @return
	 */
	private List<FileEntity> getFilesList(String filesConfig, String processInstanceId){
		List<FileEntity> filesList = new ArrayList<>();
		JSONArray filesConfigArray = JSONObject.parseArray(filesConfig);
		LambdaQueryWrapper<FileEntity> fileQuery = Wrappers.lambdaQuery();
		fileQuery
			.eq(FileEntity::getDeleted,false)
			.eq(FileEntity::getBusinessKey,processInstanceId);
		List<FileEntity> files = fileService.list(fileQuery);
		for (int i = 0; i < filesConfigArray.size(); i++) {
			JSONObject filesConfigJson = filesConfigArray.getJSONObject(i);
			String fileType = filesConfigJson.getString("fileType");
			for (FileEntity file : files) {
				if (file.getFileType().equals(fileType)){
					filesList.add(file);
				}
			}
		}
		return filesList;
	}





}




